﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;

namespace haptic.src.engine.animation
{
    public interface INodeAnimation
    {
        void GetFrameInfo(float fTime, out int iNodeIndex, out Vector2 vTranslation, out float fRotation);
    }

    public class KeyframeNodeAnimation : INodeAnimation
    {
        #region Constructors
        public KeyframeNodeAnimation(KeyframeNodeAnimationData oData)
        {
            m_oData = oData;
        }
        #endregion
        private struct KeyCacheInfo
        {
            public KeyCacheInfo(int iKeyIndex)
            {
                m_iKeyIndex = iKeyIndex;
            }
            public int m_iKeyIndex;
        }
        public void GetFrameInfo(float fTime, out int iNodeIndex, out Vector2 vTranslation, out float fRotation)
        {
            Debug.Assert(m_oData != null);
            iNodeIndex = m_oData.iNodeIndex;
            vTranslation = (Vector2)m_oData.m_oTranslations.GetValue(oCachedTransInfo.m_iKeyIndex, fTime, out oCachedTransInfo.m_iKeyIndex);
            fRotation = (float)m_oData.m_oRotations.GetValue(oCachedRotInfo.m_iKeyIndex, fTime, out oCachedRotInfo.m_iKeyIndex);
        }

        #region Attributes
        private KeyframeNodeAnimationData m_oData;
        private KeyCacheInfo oCachedTransInfo = new KeyCacheInfo(0);
        private KeyCacheInfo oCachedRotInfo = new KeyCacheInfo(0);
        #endregion
    }

    public class PhysicsNodeAnimation : INodeAnimation
    {
        #region Constructors
        public PhysicsNodeAnimation(PhysicsNodeAnimationData oData)
        {
            m_oData = oData;
        }
        #endregion

        public void GetFrameInfo(float fTime, out int iNodeIndex, out Vector2 vTranslation, out float fRotation)
        {
            iNodeIndex = m_iNodeIndex;
            vTranslation = m_vTargetTranslation;
            fRotation = m_fTargetRotation;
        }

        #region Attributes
        private PhysicsNodeAnimationData m_oData;
        private Vector2 m_vTargetTranslation = Vector2.Zero;
        private float m_fTargetRotation = 0.0f;
        private int m_iNodeIndex = 0;
        #endregion
    }
}
